'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 购买挑战次数
 * @author agl
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../../consts/code');
var moduleType = require('../../../../consts/moduleType');
var operationType = require('../../../../consts/operationType');
var configUtil = require('../../../../util/configUtil');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var dataApi = require('../../../../libs/dataApi.js');
var vipConf = require('../../../../config/vipConf.js');

var huntModel = require('../../../../models/huntModel.js');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['arena', 'build', 'role', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var arena_model = player_manager.arena;
                var role_model = player_manager.role;
                var buy_time = arena_model.buy_fight_times + 1;
                if (buy_time > dataApi.arena_time.length()) {
                    // 最高消费
                    buy_time = dataApi.arena_time.length();
                }

                // 购买次数上限判断
                var arena_challenge_times_buy = constantUtil.value('arena_challenge_times_buy');
                var buy_limit = arena_challenge_times_buy;
                if (role_model.vip > 0) {
                    var vip_conf = vipConf.getConf(role_model.vip);
                    if (!!vip_conf) {
                        var add_arena_challenge_times = vip_conf.add_arena_challenge_times;
                        buy_limit += add_arena_challenge_times;
                    }
                }
                if (buy_time > buy_limit) {
                    return next(null, {
                        code: Code.ARENA_BUY_TIME_LIMIT
                    });
                }

                var sub_diamond = dataApi.arena_time.findById(buy_time).point;
                if (!roleService.subDiamond(player_manager, moduleType.MDL_ARENA, operationType.OPR_ARENA_BUY_FIGHT_TIME, sub_diamond)) {
                    // 钻石够不够
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }
                arena_model.remain_fight_times += 1;
                arena_model.buy_fight_times = buy_time;

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }

            return next(null, {
                code: Code.OK,
                result: {}
            });
        }
    );
};
